package com.example.chatplatform.service.impl;

import com.example.chatplatform.mapper.MessageMapper;
import com.example.chatplatform.model.entity.CronyMessage;
import com.example.chatplatform.model.entity.GroupMessage;
import com.example.chatplatform.model.vo.*;
import com.example.chatplatform.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    public MessageMapper messageMapper;

    @Override
    public MessageListVO messageList(Integer userId) {
        List<CronyMessageListVO> cronyMessageList = messageMapper.cronyMessageList(userId);
        List<GroupMessageListVO> groupMessageList = messageMapper.groupMessageList(userId);

        return getMessageListVO(cronyMessageList, groupMessageList);
    }

    @Override
    public MessageListVO searchChat(Integer userId, String searchChatValue) {
        List<CronyMessageListVO> cronyMessageList = messageMapper.searchCronyChat(userId, searchChatValue);
        List<GroupMessageListVO> groupMessageList = messageMapper.searchGroupChat(userId, searchChatValue);

        return getMessageListVO(cronyMessageList, groupMessageList);
    }

    private MessageListVO getMessageListVO(List<CronyMessageListVO> cronyMessageList, List<GroupMessageListVO> groupMessageList) {
        DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("M-d");
        for (CronyMessageListVO item : cronyMessageList) {
            if (item != null) {
                LocalDateTime date = LocalDateTime.parse(item.getMessageTime(), inputFormat);
                item.setMessageTimeMD(date.format(outputFormat));
            }
        }
        for (GroupMessageListVO item : groupMessageList) {
            if (item != null) {
                LocalDateTime date = LocalDateTime.parse(item.getMessageTime(), inputFormat);
                item.setMessageTimeMD(date.format(outputFormat));
            }
        }

        return new MessageListVO(cronyMessageList, groupMessageList);
    }

    @Override
    public CronyMessageVO cronyChatRecord(Integer userId, Integer cronyId) {

        CronyMessageVO cronyMessageVO = messageMapper.cronyInfo(cronyId);

        cronyMessageVO.setUserId(userId);
        cronyMessageVO.setCronyId(cronyId);

        List<CronyMessage> cronyMessageList0 = messageMapper.cronyMessageList0(userId, cronyId);
        List<CronyMessage> cronyMessageList1 = messageMapper.cronyMessageList1(userId, cronyId);

        // 设置messageMark标记
        cronyMessageList0.forEach(item -> item.setMessageMark(0));
        cronyMessageList1.forEach(item -> item.setMessageMark(1));

        // 合并两个List
        List<CronyMessage> combinedList = new ArrayList<>();
        combinedList.addAll(cronyMessageList0);
        combinedList.addAll(cronyMessageList1);

        cronyMessageVO.setCronyMessageList(combinedList);

        return cronyMessageVO;
    }

    @Override
    public GroupMessageVO groupChatRecord(Integer userId, Integer groupId) {
        GroupMessageVO groupMessageVO = messageMapper.groupInfo(groupId);

        List<GroupMessage> groupMessageList0 = messageMapper.groupMessageList0(userId, groupId);
        List<GroupMessage> groupMessageList1 = messageMapper.groupMessageList1(userId, groupId);

        // 设置messageMark标记
        groupMessageList0.forEach(item -> item.setMessageMark(0));
        groupMessageList1.forEach(item -> item.setMessageMark(1));

        // 合并两个List
        List<GroupMessage> combinedList = new ArrayList<>();
        combinedList.addAll(groupMessageList0);
        combinedList.addAll(groupMessageList1);

        groupMessageVO.setGroupMessageList(combinedList);

        return groupMessageVO;
    }

}
